package cn.aishua.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataProvider{
	
	class DataHelper extends SQLiteOpenHelper{
		
		private static final String DB_NAME = "kaka_.db";
		private static final int VERSION = 1;

		public DataHelper(Context context) {
			super(context, DB_NAME, null, VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + TABLE_FAVORITE + " (" +
					TABLE_FAVORITE_VID + " INTEGER PRIMARY KEY)");
			db.execSQL("CREATE TABLE " + TABLE_TEMP_PIC + " (" +
					TABLE_TEMP_PIC_ID + " INTEGER PRIMARY KEY," + 
					TABLE_TEMP_PIC_DATA + " BLOB )");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		}
	}
	
	private Context mContext;
	private DataHelper mHelper;
	private static DataProvider self;
	
	public synchronized static DataProvider getInstance(Context context){
		if(self == null){
			self = new DataProvider(context);
		}
		return self;
	}
	
	private DataProvider(Context context){
		mContext = context;
		mHelper = new DataHelper(mContext);
	}
	
//-------------------------------- Table TempPic --------------------------------// 
	private static final String TABLE_TEMP_PIC = "temp_pic";
	private static final String TABLE_TEMP_PIC_ID = "id";
	private static final String TABLE_TEMP_PIC_DATA = "data";
	
	public synchronized boolean insertTempPic(int id, byte[] data){
		ContentValues values = new ContentValues();
		values.put(TABLE_TEMP_PIC_ID, id);
		values.put(TABLE_TEMP_PIC_DATA, data);
		
		SQLiteDatabase db = mHelper.getWritableDatabase();
		long retVal = db.insert(TABLE_TEMP_PIC, null, values);
		if(retVal == -1)
			return false;
		return true;
	}
	
	public void clearTempPic(){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		db.delete(TABLE_TEMP_PIC, null, null);
	}
	
	public synchronized byte[] tryGetTempPic(int id){
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor ret = db.query(TABLE_TEMP_PIC, new String[]{TABLE_TEMP_PIC_DATA}, "id=?", 
				new String[]{String.valueOf(id)}, null, null, null);
		
		android.util.Log.e("test", "data count " + ret.getCount() + ", columns " + ret.getColumnCount() + " for id "
				+ id);
		if(ret.getCount() != 1)
			return null;
		
		ret.moveToFirst();
		byte[] r = ret.getBlob(0);
		ret.close();
		return r;
	}
	
	
//-------------------------------- Table TempPic end -----------------------------//
	
//-------------------------------- Table Favorite --------------------------------// 
	private static final String TABLE_FAVORITE = "favorite";
	private static final String TABLE_FAVORITE_VID = "_id";
	
	public boolean insertFavorite(int id){
		ContentValues values = new ContentValues();
		values.put(TABLE_FAVORITE_VID, id);
		
		SQLiteDatabase db = mHelper.getWritableDatabase();
		long retVal = db.insert(TABLE_FAVORITE, null, values);
		if(retVal == -1)
			return false;
		return true;
	}
	
	public int deleteFavorite(int id) {
		SQLiteDatabase db = mHelper.getWritableDatabase();
		return db.delete(TABLE_FAVORITE, "id=?", new String[]{String.valueOf(id)});
	}
	
	public Cursor queryFavorite(){
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor ret = db.query(TABLE_FAVORITE, new String[]{TABLE_FAVORITE_VID}, null, 
							null, null, null, null);
		return ret;
	}
//-------------------------------- Table Favorite end ------------------------------//

}
